<template>

  <div v-loading="loading" element-loading-text="文件上传中......">

    <!--查询表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="24">
            <el-form-item label="会诊时间">
              <el-date-picker
                v-model="createTimes"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="margin-right: 10px;width: 100%;"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData()">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <div class="tools-div">
      <!--添加按钮-->
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
    </div>

    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form ref="dataForm" :model="sysOrder" label-width="150px" size="small" style="padding-right: 40px;">


        <el-form-item label="医生名称" prop="licenseNumber">
          <el-select v-model="sysOrder.licenseNumber" placeholder="请选择输医生名称">
            <el-option
              v-for="item in this.doctorList"
              :key="item.id"
              :label="item.name"
              :value="item.licenseNumber"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户名称" prop="userId">
          <el-select v-model="sysOrder.userId" placeholder="请选择用户名称">
            <el-option
              v-for="item in this.userList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="宠物种类:名称" prop="consultationId">
          <el-select v-model="sysOrder.pid" placeholder="请选择宠物名称:种类">
            <el-option
              v-for="item in filteredPetList"
              :key="item.id"
              :label="item.name + ':' +  item.type +':'+item.variety"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="问诊金额">
          <el-input v-model="sysOrder.amount" type="number"/>
        </el-form-item>


      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;"
      @selection-change="handleSelectionChange">

      <!--&lt;!&ndash; 复选框 &ndash;&gt;-->
      <!--<el-table-column type="selection"/>-->

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="consultationId" label="会诊编号"/>

      <el-table-column prop="licenseNumber" label="医生名称" :formatter="doctorFormatter"/>

      <el-table-column prop="userId" label="用户名称" :formatter="userFormatter"/>

      <el-table-column prop="pid" label="宠物种类:名称" :formatter="petFormatter"/>

      <el-table-column prop="amount" label="问诊金额"/>

      <!--<el-table-column prop="licenseNumber" label="执业编号"/>-->
      <!--<el-table-column prop="prescriptionPermission" label="处方许可"/>-->
      <!--<el-table-column prop="practice" label="执业"/>-->
      <!--<el-table-column prop="phoneNumber" label="联系电话"/>-->
      <!--<el-table-column prop="expertise" label="擅长领域"/>-->

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)" title="修改"/>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除"/>
        </template>
      </el-table-column>
    </el-table>


    <!--分页组件-->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />


  </div>

</template>

<script>

  import api from '@/api/sysOrder/sysOrder.js'
  import userapi from "@/api/user/user.js";
  import doctorapi from "@/api/sysDoctor/sysDoctor.js";
  import petapi from "@/api/sysPet/sysPet.js";

  export default {
    data() {
      return {
        // listLoading:true, //  加载中的提示语.....
        list: [],
        total: 0,
        page: 1,
        limit: 4,
        createTimes: [],
        searchObj: {},
        sysDoctor: {},
        sysOrder: {},
        dialogVisible: false,
        // 选中的数据
        selectValueData: [],
        fileList: [],
        loading: false,

        dialogVisible2: false,
        expertiseItem: '',

        userList: [],
        doctorList: [],
        petList: [],

      };
    },

    created() {
      this.fetchData();
      this.findAll();
    },


    computed: {
      filteredPetList() {
        if (this.sysOrder.userId === '' || this.sysOrder.userId === null) {
          return []; // 如果没有选定userId，则返回空数组
        } else {
          // 根据选定的userId过滤doctorList
          return this.petList.filter((item) => item.userId === this.sysOrder.userId);
        }
      },
    },


    methods: {

      //根据id获取用户
      userFormatter(row) {
        let id = row.userId;
        let name = '';
        for (let i = 0; i < this.userList.length; i++) {
          if (this.userList[i].id === id) {
            name = this.userList[i].name;
            break;
          }
        }
        return name;
      },

      doctorFormatter(row) {
        let licenseNumber = row.licenseNumber;
        let name = '';
        for (let i = 0; i < this.doctorList.length; i++) {
          if (this.doctorList[i].licenseNumber === licenseNumber) {
            name = this.doctorList[i].name;
            break;
          }
        }
        return name;
      },

      petFormatter(row) {
        let id = row.pid;
        let name = '';
        for (let i = 0; i < this.petList.length; i++) {
          if (this.petList[i].id === id) {
            name = this.petList[i].name + ':' + this.petList[i].type +':'+this.petList[i].variety;
            break;
          }
        }
        return name;
      },

      findAll() {
        userapi.findAll().then((response) => {
          this.userList = response.data;
        });
        doctorapi.findAll().then((response) => {
          this.doctorList = response.data;
        });
        petapi.findAll().then((response) => {
          this.petList = response.data;
        });
      },


      //分页查询
      fetchData(pageNum = 1) {
        this.page = pageNum;

        if (this.createTimes && this.createTimes.length === 2) {
          this.searchObj.createTimeBegin = this.createTimes[0];
          this.searchObj.createTimeEnd = this.createTimes[1];
        }

        console.log(this.searchObj);
        api.getPageList(this.page, this.limit, this.searchObj).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
        })
      },

      //触发重置
      resetData() {
        // 清空表单
        this.searchObj = {};
        this.createTimes = [],
          // 再次刷新列表数据
          this.fetchData();
      },


      //删除
      removeDataById(id) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(response1 => {
          api.removeDataById(id).then(response2 => {

            this.$message.success("删除成功");
            // 刷新页面
            this.fetchData();

          });
        });
      },

      // 打开弹框
      add() {
        // 将 控制弹框的 属性设置为 true
        this.dialogVisible = true;
        // 清空表单
        this.sysOrder = {};

      },

      // 根据id去回显数据
      edit(id) {
        //1.弹框
        this.dialogVisible = true;
        //2.赋值
        api.getSysDataById(id).then(response => {
          this.sysOrder = response.data;
        })
      },

      // 添加或者修改 具体功能
      saveOrUpdate() {

        if (this.sysOrder.id != null) {
          this.updateData();
        } else {
          this.addData();
        }
      },

      // 实现添加功能
      addData() {
        api.saveData(this.sysOrder)
          .then(response => {
            this.$message.success("添加成功!");
            // 关闭弹框
            this.dialogVisible = false;
            // 刷新数据
            this.fetchData();

          })
      },

      updateData() {
        api.updateData(this.sysOrder)
          .then(response => {
            this.$message.success("修改成功!");
            // 关闭弹框
            this.dialogVisible = false;
            // 刷新数据
            this.fetchData();
          })
      },


    }
  }
</script>

<style scoped>

</style>

